{extend name="base" /}
{block name="content"}
<script language="javascript" type="text/javascript" src="__STATIC__/public/My97DatePicker/WdatePicker.js"></script>
<div class="g-main">
    <div class="m-main-title">
        <div class="m-container-fluid">
            <a href="{:url('index/welcome')}"><i class="icon icon-home"></i>首页</a>
            <span class="icon-angle-right"></span>
            <span>商城</span>
            <span class="icon-angle-right"></span>
            <span>优惠券管理</span>
            <span class="icon-angle-right"></span>
            <span>优惠券列表</span>
            <span class="icon-angle-right"></span>
            <span>发布优惠券</span>
            <a class="u-flush"><i class="icon-repeat"></i></a>
        </div>
    </div>
    <div class="m-main-content">
        <div class="m-container">
            <div class="m-form">
                <form class="layui-form">
                    <table class="m-table">
                        <tr>
                            <td><label for="">发布人员<em>*</em></label></td>
                            <td>
                                <div class="lay-select m-select-def">
                                    <select name="type" lay-filter="category_id"  lay-verify="required">
                                        <option value="">请选择人员</option>
                                        <option value="1">员工</option>
                                        <option value="2">用户</option>
                                    </select>
                                </div>
                            </td>
                        </tr>
                        <tr id="yh">
                            <td><label for="">选择用户<em>*</em></label></td>
                            <td>
                                <div class="privilege-item lay-checked clearfix" style="margin-bottom: 0px;padding:0;border-bottom:none">
                                    <dl class="clearfix" style="border-bottom:none" id="xuanzhongs">
                                        <dt><h3 class="lay-checkbox"><input type="checkbox" class="select_item_all" lay-skin="primary" title="全选" lay-filter="h3_privilege" /></h3></dt>
                                        {foreach name="member" key="key" item="vo"}
                                        <dd class="lay-checkbox"><input class="yh" type="checkbox" name="member_id[]" id="member_id" lay-skin="primary" title="{$vo.nickname}" value="{$vo.id}"/></dd>
                                        {/foreach}
                                    </dl>
                                </div>
                            </td>
                        </tr>
                        <tr id="yg">
                            <td><label for="">选择员工<em>*</em></label></td>
                            <td>
                                <div class="privilege-item lay-checked clearfix" style="margin-bottom: 0px;padding:0;border-bottom:none">
                                    <dl class="clearfix" style="border-bottom:none" id="ygxuanzhongs">
                                        <dt><h3 class="lay-checkbox"><input type="checkbox" class="select_item_all" lay-skin="primary" title="全选" lay-filter="h3_privilege" /></h3></dt>
                                        {foreach name="member_qy" key="key" item="vo"}
                                        <dd class="lay-checkbox"><input class="yg" type="checkbox" name="member_qy_id[]" id="member_qy_id" lay-skin="primary" title="{$vo.name}" value="{$vo.id}"/></dd>
                                        {/foreach}
                                    </dl>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td width="15%"><label for="offset_price">发布数量<em>*</em></label></td>
                            <td><input  lay-verify="required" type="text" class="u-ipt u-ipt-small" name="number" id="number" placeholder="请输入优惠券发布数量" value="" /></td>
                        </tr>
                    </table>
                    <div class="m-form-btn">
                        <input type="hidden" name="id" id="id" value="{$id}">
                        <button class="u-btn" class="layui-btn" lay-submit lay-filter="formDemo">发布</button>
<!--                        <button id="j_btn" type="button" class="u-btn">发布</button>-->
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="extrajs"}
<script src="__STATIC__/public/js/jquery-ui.min.js"></script>
<script>
    $(function () {
        var keyValue;
        keyValue = $("select[name='type'] option:selected").val();
        console.log(keyValue);
        if (keyValue == 0) {
            $("#yh").hide();
            $("#yg").hide();
        }
        if (keyValue == 1) {
            $("#yg").show();
            $("#yh").hide();
        }
        if (keyValue == 2) {
            $("#yg").hide();
            $("#yh").show();
        }
    })
    layui.use('form', function () {
        var form = layui.form;
        form.on('select', function (data) {
            if (data.value == 0) {
                $("#yh").hide();
                $("#yg").hide();
            }
            if (data.value  == 1) {
                $("#yg").show();
                $("#yh").hide();
            }
            if (data.value  == 2) {
                $("#yg").hide();
                $("#yh").show();
            }
            form.render();
        });
        //监听提交
        form.on('submit(formDemo)', function(data){
            var member_id = [];
            //员工
            if (data.field.type == 1){
                $(".yg:checked").each(function () {
                    member_id.push($(this).val());
                });
                if (member_id.length ===0){
                    layer.msg("请选择员工");
                    return false;
                }
            }
            //用户
            if (data.field.type == 2){
                $(".yh:checked").each(function () {
                    member_id.push($(this).val());
                });
                if (member_id.length ===0){
                    layer.msg("请选择用户");
                    return false;
                }
            }
            var loading = layer.msg('正在发劵····', {icon: 16, shade: 0.3, time:0});
            $.post('/manage/coupon/release_coupon',{
                id : data.field.id,
                type : data.field.type,
                number : data.field.number,
                member_id :member_id,
            },function (res){
                layer.close(loading);
                layer.msg(res.message);
            })
            return false;
        });
    });
</script>
{/block}